package com.ruoyi.yyq.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.auth.form.RegisterBody;
import com.ruoyi.auth.service.SysLoginService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.yyq.domain.CarMerchantVerification;
import com.ruoyi.yyq.service.ICarMerchantVerificationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商户认证Controller
 * 
 * @author ruoyi
 * @date 2025-05-12
 */
@RestController
    @RequestMapping("/verification")
@CrossOrigin
public class CarMerchantVerificationController extends BaseController
{
    @Autowired
    private ICarMerchantVerificationService carMerchantVerificationService;
    @Autowired
    private RemoteUserService remoteUserService;
    /**
     * 注册
     */
    /**
     * 查询商户认证列表
     */
    @RequiresPermissions("verification:verification:list")
    @GetMapping("/list")
    public TableDataInfo list(CarMerchantVerification carMerchantVerification)
    {
        startPage();
        List<CarMerchantVerification> list = carMerchantVerificationService.selectCarMerchantVerificationList(carMerchantVerification);
        return getDataTable(list);
    }

    /**
     * 导出商户认证列表
     */
    @RequiresPermissions("verification:verification:export")
    @Log(title = "商户认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarMerchantVerification carMerchantVerification)
    {
        List<CarMerchantVerification> list = carMerchantVerificationService.selectCarMerchantVerificationList(carMerchantVerification);
        ExcelUtil<CarMerchantVerification> util = new ExcelUtil<CarMerchantVerification>(CarMerchantVerification.class);
        util.exportExcel(response, list, "商户认证数据");
    }

    /**
     * 获取商户认证详细信息
     */
    @RequiresPermissions("verification:verification:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carMerchantVerificationService.selectCarMerchantVerificationById(id));
    }


    /**
     * 获取商户认证详细信息
     */
    @RequiresPermissions("verification:verification:query")
    @GetMapping(value = "/selectbymobile/{mobile}")
    public AjaxResult getInfo2(@PathVariable("mobile") String mobile)
    {
        return success(carMerchantVerificationService.selectCarMerchantVerificationByMobile(mobile));
    }
    /**
     * 新增商户认证
     */
    @RequiresPermissions("verification:verification:add")
    @Log(title = "商户认证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarMerchantVerification carMerchantVerification)
    {
        int i = carMerchantVerificationService.insertCarMerchantVerification(carMerchantVerification);
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(carMerchantVerification.getId());
        sysUser.setUserName(carMerchantVerification.getMobile());
        sysUser.setNickName(carMerchantVerification.getDirector());
        sysUser.setPassword("123456");
        sysUser.setPhonenumber(carMerchantVerification.getMobile());
        sysUser.setStatus("0");
        remoteUserService.add(sysUser, SecurityConstants.INNER);
        SysUser sysUser1 = remoteUserService.getUserInfo2(carMerchantVerification.getMobile(), SecurityConstants.INNER);
        System.out.println(sysUser1);
        Long userId = sysUser1.getUserId();
        remoteUserService.insertAuthRole(userId, new Long[]{100L}, SecurityConstants.INNER);
        return toAjax(i);
    }

    /**
     * 新增商户认证
     */
//    @RequiresPermissions("verification:verification:add")
    @Log(title = "商户认证", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody CarMerchantVerification carMerchantVerification)
    {
        int i = carMerchantVerificationService.insertCarMerchantVerification(carMerchantVerification);
        Long deptId = carMerchantVerification.getId();
        return AjaxResult.success(deptId);
    }
    /**
     * 修改商户认证
     */
    @RequiresPermissions("verification:verification:edit")
    @Log(title = "商户认证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarMerchantVerification carMerchantVerification)
    {

        return toAjax(carMerchantVerificationService.updateCarMerchantVerification(carMerchantVerification));
    }

    /**
     * 删除商户认证
     */
    @RequiresPermissions("verification:verification:remove")
    @Log(title = "商户认证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carMerchantVerificationService.deleteCarMerchantVerificationByIds(ids));
    }
}
